package com.tea.room.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tea.room.model.Return;
import com.tea.room.model.tea.Teahouse;
import com.tea.room.service.tea.HouseInfoService;
import com.tea.room.service.tea.SelectionService;
import com.tea.room.service.tea.TeaOrderService;
import com.tea.room.service.tea.TeahouseService;
import com.tea.room.util.ConstantUtil;

/**
 * 茶楼基本信息操作
 * @author Administrator
 *
 */
@RestController("mainGateController")
public class MainGateController {

    @Autowired
    private SelectionService selectionService;

    @Autowired
    private TeahouseService teahouseService;

    @Autowired
    private TeaOrderService teaOrderService;
    
    @Autowired
    private HouseInfoService houseInfoService;

    /**
     * 查询茶楼列表
     * 
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryTeahouse")
    public List<Teahouse> queryTeahouse(HttpServletRequest request, HttpServletResponse response) {
        List<Teahouse> list = teahouseService.findTeahouse();
        return list;
    }
    
    /**
     * 查询茶楼详细信息
     * 
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryTeahouseInfo")
    public Map<String, Object> queryTeahouseById(HttpServletRequest request, HttpServletResponse response) {
    	String roomId = request.getParameter("roomId");
    	String openid = request.getParameter("openid");
    	Map<String, Object> houseInfo = houseInfoService.findHouseInfo(roomId, openid);
        return houseInfo;
    }

    /**
     * 查询茶楼的已售时间点
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    @ResponseBody
    @RequestMapping(value = "/getReserveTime")
    public Object getReserveTime(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String tea_code = request.getParameter("roomId");
        Map<String,Object> map = new HashMap<String, Object>();
        if(tea_code == null || tea_code.equals("")){
            map.put("flag", ConstantUtil.ERROR_FLAG);
            map.put("message", "roomId 为空！");
            return map;
        }
        List<Map<String, Object>> list = selectionService.findSelection(tea_code);
        return list;
    }

    /**
     * 保存订单，返回订单号
     * 
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveOrder")
    public Map<String, Object> saveOrder(HttpServletRequest request, HttpServletResponse response) {
        String tea_code = request.getParameter("roomId");//茶室id
        String times = request.getParameter("times");//时间段
        String price = request.getParameter("price");//订单金额
        String total = request.getParameter("total");//总计时长
        Map<String,Object> map = teaOrderService.saveOrder(tea_code, times, price, total);
        System.out.println(map);
        return map;
    }
    
    /**
     * 查询订单
     * 
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryOrderByLimit")
    public Return queryOrder(HttpServletRequest request, HttpServletResponse response) {
        Return order = teaOrderService.queryOrderByLimit(request);
        return order;
    }

}
